<?php
/*
 * @Author: xiadc
 * @Date: 2021-04-21 08:22:35
 * @LastEditors: xiadc
 * @LastEditTime: 2021-09-26 13:32:46
 */
/**
 * *
 *  * ============================================================================
 *  * Created by PhpStorm.
 *  * User: Ice
 *  * 邮箱: ice@sbing.vip
 *  * 网址: https://sbing.vip
 *  * Date: 2019/9/19 下午3:33
 *  * ============================================================================.
 */

namespace app\admin\controller;

use think\facade\Config;
use app\admin\model\Opus;
use app\common\model\User;
use app\common\controller\Backend;

/**
 * 控制台.
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    /**
     * 查看.
     */
    public function index()
    {
        $dataday = config('site.dataday');
        $seventtime = \fast\Date::unixtime('day', -$dataday);
        $config = config('site.channel');
        $userlist = $opuslist = [];
        for ($i = 0; $i < $dataday; $i++) {
            $start = $seventtime + (($i+1) * 86400);
            $end = $seventtime + (($i+2) * 86400);
            $day = date('Y-m-d', $start);
            $userlist['day'][] = $day;
            $opuslist['day'][] = $day;
            $userlist['column'] = $opuslist['column'] = array_keys($config);
            $users = User::where('createtime','>=',$start)->where('createtime','<',$end)->select();
            $opus = Opus::where('createtime','>=',$start)->where('createtime','<',$end)->select();
            foreach ($config as $k => $v) {
                if (!isset($userlist['list'][$k])) {
                    $userlist['list'][$k]['name'] = $k;
                    $opuslist['list'][$k]['name'] = $k;
                }
                $num1 = $num2 = 0;
                foreach ($users as $u){
                    if ($u->channel == $v) {
                        $num1++;
                    }
                }
                foreach ($opus as $o){
                    if ($o->channel == $v) {
                        $num2++;
                    }
                }
                $userlist['list'][$k]['data'][$day] = $num1;
                $opuslist['list'][$k]['data'][$day] = $num2;
            }
        }
        $userlist['list'] = array_values($userlist['list']);
        $opuslist['list'] = array_values($opuslist['list']);
        foreach ($userlist['list'] as &$list){
            $list['data'] = array_values( $list['data']);
        }
        foreach ($opuslist['list'] as &$list){
            $list['data'] = array_values( $list['data']);
        }
        // print_r(json_encode($opuslist) );die;
        $this->view->assign([
            'totaluser'       => User::count(),
            'todayuser'       => User::where('createtime','>=',strtotime(date('Y-m-d')))->count(),
            'totalopus'       => Opus::count(),
            'todayopus'       => Opus::where('createtime','>=',strtotime(date('Y-m-d')))->count(),
            'userlist'        => $userlist,
            'opuslist'        => $opuslist
        ]);

        return $this->view->fetch();
    }
}
